package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.cache.jetcache.utils.JetCacheUtils;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.util.TreeUtil;
import com.bifang.module.base.common.vo.operationPeriod.OperationCurVO;
import com.bifang.module.base.common.vo.operationPeriod.OperationPeriodVO;
import com.bifang.module.base.common.vo.tree.TreeVO;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.base.core.integration.OperationPeriodIntegration;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.constant.EduCache;
import com.jwsoft.manager.common.vo.eduFormSpecial.EduFormSpecialKeyVO;
import com.jwsoft.manager.common.vo.eduFormSpecial.EduFormSpecialQueryVO;
import com.jwsoft.manager.common.vo.eduFormSpecial.EduFormSpecialVO;
import com.jwsoft.manager.common.vo.eduFormSpecialParams.EduFormSpecialParamsQueryVO;
import com.jwsoft.manager.common.vo.eduFormSpecialParams.EduFormSpecialParamsVO;
import com.jwsoft.manager.core.dao.model.EduEnrollmenBrochure;
import com.jwsoft.manager.core.dao.model.EduFormSpecial;
import com.jwsoft.manager.core.dao.model.EduFormSpecialParams;
import com.jwsoft.manager.core.dao.service.EduEnrollmenBrochureService;
import com.jwsoft.manager.core.dao.service.EduFormSpecialParamsService;
import com.jwsoft.manager.core.dao.service.EduFormSpecialService;
import com.jwsoft.manager.core.integration.EduFormSpecialIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 特殊通道表业务实现类
 *
 * @author wangtao
 * @since 2022-12-06
 */
@ApiService(funcCode = "eduFormSpecial", title = "特殊通道表")
public class EduFormSpecialIntegrationImpl implements EduFormSpecialIntegration {

    @Autowired
    public EduFormSpecialService eduFormSpecialService;

    @Resource
    OperationPeriodIntegration operationPeriodIntegration;

    @Autowired
    EduFormSpecialParamsService eduFormSpecialParamsService;

    @Autowired
    DictHelper dictHelper;

    @Autowired
    public EduHelper eduHelper;

    @Autowired
    public EduEnrollmenBrochureService eduEnrollmenBrochureService;


    @Override
    @OpApi(funcCode = "eduFormSpecial0001", title = "特殊通道表分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduFormSpecialVO> getList(EduFormSpecialQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EduFormSpecialVO> list = eduFormSpecialService.getList(vo);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduFormSpecialVO> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduFormSpecialVO> resultList = Convert.toList(EduFormSpecialVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduFormSpecialJh0001", title = "政策性安排分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduFormSpecialVO> getListJh(EduFormSpecialQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        if(vo.getYear()==null){
            vo.setYear(eduHelper.thisYear());
        }
        if(StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(SessionUtil.getAreaCode());
        }
        QueryWrapper<EduFormSpecial> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(StringUtils.isNotBlank(vo.getAreaCode()),EduFormSpecial::getAreaCode,vo.getAreaCode())
                .eq(StringUtils.isNotBlank(vo.getBigClassValue()),EduFormSpecial::getBigClassValue,vo.getBigClassValue())
                .eq(StringUtils.isNotBlank(vo.getSmallClassValue()),EduFormSpecial::getSmallClassValue,vo.getSmallClassValue())
                .eq(StringUtils.isNotBlank(vo.getStatus()),EduFormSpecial::getStatus,vo.getStatus())
                .eq(vo.getYear()!=null,EduFormSpecial::getYear,vo.getYear());
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EduFormSpecial> list = eduFormSpecialService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduFormSpecial> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduFormSpecialVO> resultList = Convert.toList(EduFormSpecialVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0002", title = "特殊通道表根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduFormSpecialVO getById(EduFormSpecialKeyVO vo) {
        EduFormSpecial entity = eduFormSpecialService.getById(vo.getFormSpecialId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduFormSpecialVO result = Convert.convert(EduFormSpecialVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduFormSpecial0003", title = "特殊通道表保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduFormSpecialVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0004", title = "特殊通道表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduFormSpecial0004",
            operationName = "特殊通道表新增",
            dataType = "formSpecialId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduFormSpecialVO add(EduFormSpecialVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduFormSpecial formSpecial = Convert.convert(EduFormSpecial.class, vo);
        if (ObjectUtils.isEmpty(formSpecial.getFormSpecialId())) {
            formSpecial.setFormSpecialId(IdUtil.simpleUUID());
        }
        int year;
        OperationCurVO curVO = new OperationCurVO();
        curVO.setOperating("XN");
        OperationPeriodVO operationPeriodVO =
                operationPeriodIntegration.getCurrentByOperating(curVO);
        if (operationPeriodVO != null) {
            year = operationPeriodVO.getPeriodYear();
        } else {
            year = DateUtil.thisYear();
        }
        if (StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(SessionUtil.getAreaCode());
        }
        if (StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(eduHelper.thisAreaCode());
        }
        QueryWrapper<EduFormSpecial> checkQueryWrapper = new QueryWrapper<>();
        checkQueryWrapper
                .lambda()
                .eq(EduFormSpecial::getYear, year)
                .eq(EduFormSpecial::getStatus, BoolEnum.TRUE.getType())
                .eq(EduFormSpecial::getBigClassValue, vo.getBigClassValue())
                .eq(StringUtils.isNotBlank(vo.getAreaCode()),EduFormSpecial::getAreaCode,vo.getAreaCode())
                .ne(
                        !ObjectUtils.isEmpty(vo.getFormSpecialId()),
                        EduFormSpecial::getFormSpecialId,
                        vo.getFormSpecialId());
        List<EduFormSpecial> formSpecialList = eduFormSpecialService.list(checkQueryWrapper);
        int maxBigKey = 0;
        int maxSmallKey = 0;
        if (formSpecialList.size() > 0) {
            maxBigKey = formSpecialList.get(0).getBigClassKey();
            maxSmallKey = formSpecialList.get(0).getSmallClassKey();
            for (EduFormSpecial eduFormSpecial : formSpecialList) {
                if (StringUtils.equals(
                        eduFormSpecial.getSmallClassValue(), formSpecial.getSmallClassValue())) {
                    throw new AppException("当前年度小类重复");
                }
                if (eduFormSpecial.getOrderNo().equals(formSpecial.getOrderNo())) {
                    throw new AppException("当前排序号重复");
                }
                if (!ObjectUtils.isEmpty(vo.getFormSpecialId())) {
                    EduFormSpecial oldFormSpecial = eduFormSpecialService.getById(vo.getFormSpecialId());
                    maxSmallKey = oldFormSpecial.getSmallClassKey();
                    break;
                }
                maxSmallKey =
                        (maxSmallKey < eduFormSpecial.getSmallClassKey()
                                ? eduFormSpecial.getSmallClassKey()
                                : maxSmallKey)
                                + 1;
            }
        } else {
            QueryWrapper<EduFormSpecial> allQueryWrapper = new QueryWrapper<>();
            allQueryWrapper
                    .lambda()
                    .eq(EduFormSpecial::getYear, year)
                    .eq(EduFormSpecial::getStatus, BoolEnum.TRUE.getType())
                    .ne(
                            !ObjectUtils.isEmpty(vo.getFormSpecialId()),
                            EduFormSpecial::getFormSpecialId,
                            vo.getFormSpecialId());
            List<EduFormSpecial> allSpecialList = eduFormSpecialService.list(allQueryWrapper);
            if (allSpecialList.size() > 0) {
                for (int i = 0; i < allSpecialList.size(); i++) {
                    if (allSpecialList.get(i).getOrderNo().equals(formSpecial.getOrderNo())) {
                        throw new AppException("当前排序号重复");
                    }
                    maxBigKey =
                            (maxBigKey < allSpecialList.get(i).getBigClassKey()
                                    ? allSpecialList.get(i).getBigClassKey()
                                    : maxBigKey)
                                    + 1;
                }
            } else {
                maxBigKey = 1;
            }
            maxSmallKey = maxBigKey * 100 + 1;
        }

        formSpecial.setYear(year);
        formSpecial.setBigClassKey(maxBigKey);
        formSpecial.setSmallClassKey(maxSmallKey);
        eduFormSpecialService.saveOrUpdate(formSpecial);
        //将主键设置回vo使操作日志可以取到
        vo.setFormSpecialId(formSpecial.getFormSpecialId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0005", title = "特殊通道表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduFormSpecial0005",
            operationName = "特殊通道表修改",
            dataType = "formSpecialId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduFormSpecialVO edit(EduFormSpecialVO vo) {
        if (ObjectUtils.isEmpty(vo.getFormSpecialId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduFormSpecial oldEntity = eduFormSpecialService.getById(vo.getFormSpecialId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduFormSpecial entity = Convert.convert(EduFormSpecial.class, vo);
        eduFormSpecialService.updateById(entity);
        QueryWrapper<EduEnrollmenBrochure> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(EduEnrollmenBrochure::getSchoolId, EduEnrollmenBrochure::getAreaCode).eq(EduEnrollmenBrochure::getYear, eduHelper.thisYear())
                .like(EduEnrollmenBrochure::getSpecialIds, oldEntity.getFormSpecialId());
        List<EduEnrollmenBrochure> list = eduEnrollmenBrochureService.list(queryWrapper);
        String areaCode = eduHelper.thisAreaCode();
        if (!CollectionUtils.isEmpty(list)) {
            for (EduEnrollmenBrochure brochure : list) {
                areaCode = brochure.getAreaCode();
                clearCache(brochure.getSchoolId());
                clearCacheForm(brochure.getSchoolId());
            }
        }
        clearCacheAreaCode(areaCode);
        return vo;
    }

    private void clearCache(String orgId) {
        if (StringUtils.isNotBlank(orgId)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_SCHOOL_DETAIL, CacheType.BOTH);
            if (cache != null) {
                cache.remove(orgId);
            }
        }
    }

    private void clearCacheAreaCode(String areaCode) {
        if (StringUtils.isNotBlank(areaCode)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_SCHOOL_areaCode, CacheType.BOTH);
            if (cache != null) {
                cache.remove(areaCode);
            }
        }
    }

    private void clearCacheForm(String schoolId) {
        if (StringUtils.isNotBlank(schoolId)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_ENROLL_FORM_SCHOOL, CacheType.BOTH);
            if (cache != null) {
                cache.remove(schoolId);
            }
        }
    }
    @Override
    @OpApi(funcCode = "eduFormSpecial0006", title = "特殊通道表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduFormSpecial0006",
            operationName = "特殊通道表删除",
            dataType = "formSpecialId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduFormSpecialKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getFormSpecialId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduFormSpecial entity = eduFormSpecialService.getById(vo.getFormSpecialId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduFormSpecialService.removeById(vo.getFormSpecialId());
    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0007", title = "特殊通道表根据条件查询详情", funcType = FuncTypeEnum.query, checkPrivilege = BoolEnum.FALSE)
    public EduFormSpecialVO getByConditon(EduFormSpecialQueryVO vo) {
        QueryWrapper<EduFormSpecial> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(StringUtils.isNotBlank(vo.getClassKey()),EduFormSpecial::getSmallClassKey,vo.getClassKey())
                             .eq(!ObjectUtils.isEmpty(vo.getYear()),EduFormSpecial::getYear,vo.getYear())
                              .eq(StringUtils.isNotBlank(vo.getAreaCode()), EduFormSpecial::getAreaCode,vo.getAreaCode());
        List<EduFormSpecial> eduFormSpecialList = eduFormSpecialService.list(queryWrapper);
        if (ObjectUtils.isEmpty(eduFormSpecialList)) {
            throw new AppException("查询不到详细信息!");
        }
        if (eduFormSpecialList.size()>1) {
            throw new AppException("查询到多条数据!");
        }
        EduFormSpecial entity = eduFormSpecialList.get(0);
        EduFormSpecialVO result = Convert.convert(EduFormSpecialVO.class, entity);
        return result;
    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0008", title = "查询特殊通道配置信息", funcType = FuncTypeEnum.query, checkPrivilege = BoolEnum.FALSE)
    public List<EduFormSpecialParamsVO>  getFormSpecialConfigInfo(EduFormSpecialParamsQueryVO vo) {
        String formSpecialId = vo.getFormSpecialId();
        QueryWrapper<EduFormSpecialParams> specialConfigVOQueryWrapper = new QueryWrapper<>();
        specialConfigVOQueryWrapper
                .lambda()
                .eq(EduFormSpecialParams::getFormSpecialId, formSpecialId)
                .orderByAsc(EduFormSpecialParams::getOrderNo);
        List<EduFormSpecialParams> list= eduFormSpecialParamsService.list(specialConfigVOQueryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<EduFormSpecialParamsVO> resList =
                Convert.toList(
                        EduFormSpecialParamsVO.class,list
                        );
        return resList;
    }


    @Override
    @OpApi(funcCode = "eduFormSpecial0009", title = "特殊通道树查询", funcType = FuncTypeEnum.query, checkPrivilege = BoolEnum.FALSE)
    public List<TreeVO> getFormSpecialTree(EduFormSpecialQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        Integer year = vo.getYear();
        if(year==null){
            year = eduHelper.thisYear();
        }


        vo.setYear(year);
        List<EduFormSpecialVO> list= eduFormSpecialService.getList(vo);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        PageInfo<EduFormSpecialVO> pageInfo =new PageInfo<>(list);
        List<TreeVO> treeVOList =new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        map.put("title", "title");
        for (EduFormSpecialVO eduFormSpecial : pageInfo.getList()){
            TreeVO treeVO = new TreeVO();
            treeVO.setKey(eduFormSpecial.getClassKey());
            // 选择后获取的值
            treeVO.setValue(eduFormSpecial.getClassKey());
            treeVO.setParentKey(eduFormSpecial.getParentId());
            treeVO.setTitle(eduFormSpecial.getClassValue());
            treeVO.setScopedSlots(map);
            treeVOList.add(treeVO);
        }
        treeVOList = TreeUtil.constructTaskDTOToTree(treeVOList);
        return treeVOList;

    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0010", title = "义务招生方案特殊类别查询", funcType = FuncTypeEnum.query, checkPrivilege = BoolEnum.FALSE)
    public PageInfo<EduFormSpecialVO> getFormSpecialInfo(EduFormSpecialQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        if(StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(SessionUtil.getSessionInfo().getAreaCode());
        }

        QueryWrapper<EduFormSpecial> formSpecialQueryWrapper = new QueryWrapper<>();
        formSpecialQueryWrapper
                .lambda()
                .eq(!ObjectUtils.isEmpty(vo.getYear()), EduFormSpecial::getYear, vo.getYear())
                .like(
                        !ObjectUtils.isEmpty(vo.getBigClassValue()),
                        EduFormSpecial::getBigClassValue,
                        vo.getBigClassValue())
                .like(
                        !ObjectUtils.isEmpty(vo.getSmallClassValue()),
                        EduFormSpecial::getSmallClassValue,
                        vo.getSmallClassValue())
                .eq(
                        !ObjectUtils.isEmpty(vo.getStatus()),
                        EduFormSpecial::getStatus,
                        vo.getStatus())
                .eq(!ObjectUtils.isEmpty(vo.getAreaCode()),
                        EduFormSpecial::getAreaCode,
                        vo.getAreaCode())
                .orderByAsc(EduFormSpecial::getOrderNo);

        List<EduFormSpecial> list = eduFormSpecialService.list(formSpecialQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduFormSpecial> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduFormSpecialVO> resultList = Convert.toList(EduFormSpecialVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        if (!ObjectUtils.isEmpty(vo.getEnrollmenBrochureId())) {
            EduEnrollmenBrochure brochure =
                    eduEnrollmenBrochureService.getById(vo.getEnrollmenBrochureId());
            resultList.forEach(
                    item -> {
                        if (!ObjectUtils.isEmpty(brochure.getSpecialIds())
                                && brochure.getSpecialIds().contains(item.getFormSpecialId())) {
                            item.setChecked(true);
                        } else {
                            item.setChecked(false);
                        }
                    });
        }
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0011", title = "特殊通道类别复制上年配置", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduFormSpecial0011",
            operationName = "特殊通道类别复制上年配置",
            dataType = "formSpecialId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void copyLastYearSpecial(EduFormSpecialVO vo) {
        int year = 0;
        year = eduHelper.thisYear();
        int lastYear = year - 1;
        QueryWrapper<EduFormSpecial> eduFormSpecialQueryWrapper = new QueryWrapper<>();
        eduFormSpecialQueryWrapper
                .lambda()
                .eq(EduFormSpecial::getStatus, BoolEnum.TRUE.getType())
                .eq(EduFormSpecial::getYear, lastYear);
        List<EduFormSpecial> oldFormSpecialList = eduFormSpecialService.list(eduFormSpecialQueryWrapper);
        if (CollectionUtils.isEmpty(oldFormSpecialList)) {
            throw new AppException("上年度没有数据，请新增");
        }
        for (int i = 0; i < oldFormSpecialList.size(); i++) {
            QueryWrapper<EduFormSpecial> checkQueryWrapper = new QueryWrapper<>();
            checkQueryWrapper
                    .lambda()
                    .eq(EduFormSpecial::getStatus, BoolEnum.TRUE.getType())
                    .eq(EduFormSpecial::getYear, year);
            long count = eduFormSpecialService.count(checkQueryWrapper);
            if (count > 0) {
                throw new AppException("当前年度已存在特殊通道类别");
            }
            oldFormSpecialList.get(i).setYear(year);
            oldFormSpecialList.get(i).setFormSpecialId(IdUtil.simpleUUID());
        }
        eduFormSpecialService.saveBatch(oldFormSpecialList);
    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0012", title = "特殊通道树查询", funcType = FuncTypeEnum.query, checkPrivilege = BoolEnum.FALSE)
    public List<TreeVO> getSpecialManagerTree(EduFormSpecialQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        if(vo.getYear()==null){
            vo.setYear(eduHelper.thisYear()) ;
        }

        String schoolId = SessionUtil.getSessionInfo().getOrgId();

        if(StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(SessionUtil.getSessionInfo().getAreaCode());
        }
        List<TreeVO> treeVOList =new ArrayList<>();

        QueryWrapper<EduEnrollmenBrochure> brochureQueryWrapper = new QueryWrapper<>();
        brochureQueryWrapper.lambda().eq(EduEnrollmenBrochure::getSchoolId,schoolId)
                .eq(EduEnrollmenBrochure::getYear,vo.getYear())
                .eq(EduEnrollmenBrochure::getAreaCode,vo.getAreaCode());

        EduEnrollmenBrochure brochure = eduEnrollmenBrochureService.getOne(brochureQueryWrapper,false);
        if (brochure != null&&StringUtils.isNotEmpty(brochure.getSpecialIds())) {
            String[] specialIds = StringUtils.split(brochure.getSpecialIds(),",");
            List<String> specialIdtList = new ArrayList<>(specialIds.length);
            Collections.addAll(specialIdtList, specialIds);
            vo.setFormSpecialIdList(specialIdtList);


        List<EduFormSpecialVO> list= eduFormSpecialService.getBrochureSpecialList(vo);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        PageInfo<EduFormSpecialVO> pageInfo =new PageInfo<>(list);

        Map<String,Object> map=new HashMap<>();
        map.put("title", "title");
        for (EduFormSpecialVO eduFormSpecial : pageInfo.getList()){
            TreeVO treeVO = new TreeVO();
            treeVO.setKey(eduFormSpecial.getClassKey());
            // 选择后获取的值
            treeVO.setValue(eduFormSpecial.getClassKey());
            treeVO.setParentKey(eduFormSpecial.getParentId());
            treeVO.setTitle(eduFormSpecial.getClassValue());
            treeVO.setScopedSlots(map);
            treeVOList.add(treeVO);
        }
        treeVOList = TreeUtil.constructTaskDTOToTree(treeVOList);
        return treeVOList;
        }else{
            return treeVOList;
        }


    }

    @Override
    @OpApi(funcCode = "eduFormSpecial0013", title = "根据小类名称获取特殊通道配置", funcType = FuncTypeEnum.query, checkPrivilege = BoolEnum.FALSE)
    public EduFormSpecialVO getBySmallValue(EduFormSpecialQueryVO queryVO) {
        if (StringUtils.isBlank(queryVO.getAreaCode())) {
            queryVO.setAreaCode(SessionUtil.getAreaCode());
        }
        if (StringUtils.isBlank(queryVO.getAreaCode())) {
            queryVO.setAreaCode(eduHelper.thisAreaCode());
        }
        if (queryVO.getYear() == null) {
            queryVO.setYear(eduHelper.thisYear());
        }

        return eduFormSpecialService.getBySmallValue(queryVO);
    }


}
